package cn.itcast.function;

import cn.itcast.bean.SectorBean;
import com.alibaba.fastjson.JSON;
import org.apache.flink.streaming.api.functions.windowing.RichAllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.hadoop.hbase.client.Put;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class SectorHbaseListWindowFunction extends RichAllWindowFunction<SectorBean, List<Put>, TimeWindow> {
    @Override
    public void apply(TimeWindow timeWindow, Iterable<SectorBean> iterable, Collector<List<Put>> collector) throws Exception {
        List<Put> list = new ArrayList<>();
        for (SectorBean value : iterable) {
            String rowkey=value.getSectorCode()+value.getTradeTime();
            Put put = new Put(rowkey.getBytes());
            String str = JSON.toJSONString(value);
            put.addColumn("info".getBytes(), "data".getBytes(), str.getBytes());
            list.add(put);
        }
        collector.collect(list);
    }
}
